package com.nameless.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nameless.dao.CarMapper;
import com.nameless.entity.Car;
import com.nameless.service.CarService;
import com.nameless.vo.CarVo;
import com.nameless.vo.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ：潘淼淼
 * @date ：Created in 2023/2/27 20:32
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class CarServiceImpl extends ServiceImpl<CarMapper, Car> implements CarService {
    @Resource
    private CarMapper carMapper;

    @Override
    public Result queryCars(Integer id) {
        Car car = carMapper.selectById(id);
        return new Result(200, "查询所有成功", car);
    }

    @Override
    public Car getByName(String name) {
        QueryWrapper<Car> wrapper = new QueryWrapper<>();
        wrapper.eq("carName", name);
        Car car = carMapper.selectOne(wrapper);
        return car;
    }

    @Override
    public Result queryAllCars(CarVo carVo) {
        PageHelper.startPage(carVo.getPageNum(), carVo.getPageSize());
        List<Car> car = carMapper.queryAllCar(carVo);
        PageInfo<Car> info = new PageInfo<>(car);
        return new Result(200, "查询所有成功", info);
    }

    @Override
    public Result delete(Integer id) {
        int i = carMapper.deleteById(id);
        return new Result(200, "删除成功", i);
    }

    @Override
    public Result queryAlls() {
        List<Car> cars = carMapper.selectList(null);
        return new Result(200, "查询成功", cars);
    }

    @Override
    public Result updates(Car car) {
        int i = carMapper.updateById(car);
        return new Result(200, "修改成功", i);
    }

    @Override
    public Result insert(Car car) {
        int insert = carMapper.insert(car);
        return new Result(200, "添加成功", insert);
    }

    @Override
    public List<Car> carShopIdTypeId(Integer shopId, Integer type) {
        QueryWrapper<Car> wrapper = new QueryWrapper<>();
        wrapper.eq("type",type);
        wrapper.eq("shop_id",shopId);
        List<Car> cars = carMapper.selectList(wrapper);
        return cars;
    }

    @Override
    public List<Car> carSort(Integer shopId, Integer type, Integer value) {
        //1从高大到低
        QueryWrapper<Car> wrapper = new QueryWrapper<>();
        wrapper.eq("type",type);
        wrapper.eq("shop_id",shopId);
        if (value==1){
            wrapper.orderByAsc("price");
        }else {
            wrapper.orderByDesc("price");
        }
        List<Car> cars = carMapper.selectList(wrapper);
        return cars;
    }

    @Override
    public List<Car> carConfig(Integer shopId, Integer type, Integer value) {
        QueryWrapper<Car> wrapper = new QueryWrapper<>();
        wrapper.eq("type",type);
        wrapper.eq("shop_id",shopId);
        wrapper.eq("config_id",value);
        List<Car> cars = carMapper.selectList(wrapper);
        return cars;
    }

    @Override
    public List<Car> carBrand(Integer shopId, Integer type, Integer value) {
        QueryWrapper<Car> wrapper = new QueryWrapper<>();
        wrapper.eq("type",type);
        wrapper.eq("shop_id",shopId);
        wrapper.eq("brand_id",value);
        List<Car> cars = carMapper.selectList(wrapper);
        return cars;
    }

//    @Override
//    public List<Car> getAll(Integer shopId) {
//        LambdaQueryWrapper<Car> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(Car::getShopId, shopId);
//        List<Car> list = carMapper.selectList(queryWrapper);
//        return list;
//    }

}